package com.kehutong.house.admin;

import com.kehutong.common.util.Token;
import com.kehutong.house.entity.CommunityMerchant;
import com.kehutong.house.entity.DeviceManagement;
import com.kehutong.house.entity.EquipmentClassify;
import com.kehutong.house.entity.EquipmentType;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import java.util.List;

@MvcAction
@ReqMapping("/admin/equipment/classify")
public class EquipmentClassifyController {
    @Inject
    private JdbcSession jdbcSession;
    public final static String[] EXCEL_IMPORT_HEADER = {"设备类型名称"};
    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) {
        FindPage<EquipmentClassify> findPage = jdbcSession.findPage(EquipmentClassify.class)
                .eq(jsonObject,"deviceTypeId")
                .like(jsonObject, "deviceClassifyName", "deviceClassifyName")
                .eq("deleted", false)
                .order("typeStatus", "DESC")
                .order("sequence", "ASC")
                .page(jsonObject);
        Page<EquipmentClassify> page = findPage.exe();
        page.getList().forEach(item->{
            if(Objects.nonNull(item.getDeviceTypeId())){
                EquipmentType supplierGoods=jdbcSession.findById(EquipmentType.class,item.getDeviceTypeId());
                item.setTypeClassify(supplierGoods.getDeviceTypeName());
            }
        });
        return page;
    }

    @ReqMapping("/listAll")
    public Object listAll(JSONObject jsonObject) throws Exception {
        return jdbcSession.findArray(EquipmentClassify.class)
                .eq("deleted", false)
                .eq("typeStatus", true)
                .order("createTime", "DESC")
                .exe();
    }



    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(EquipmentClassify equipmentClassify) throws Exception {
        // 增加校验不允许有相同的类型名称名称
        EquipmentClassify item = jdbcSession.findOne(EquipmentClassify.class)
                .eq("deviceClassifyName", equipmentClassify.getDeviceClassifyName())
                .noEq("id", equipmentClassify.getId())
                .eq("deleted", false)
                .exe();

        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前设备分类名称，请修改后重试");
        }
        jdbcSession.updateById(equipmentClassify);
        return Result.success();
    }

    @Bean(copy=true)
    @ReqMapping("/updateSetStatus")
    public Object updateSetStatus(EquipmentClassify equipmentClassify) throws Exception {
        jdbcSession.updateById(equipmentClassify);
        //当状态改为关闭时，需要改对应的设备状态为不可用
        if(equipmentClassify.getTypeStatus()==false){
            List<DeviceManagement> list = jdbcSession.findArray(DeviceManagement.class)
                    .eq("equipmentType",equipmentClassify.getId())
                    .eq("deleted", false)
                    .exe();
            list.forEach(item->{
                item.setState(false);
                jdbcSession.updateById(item);
            });
        }
        return Result.success();
    }
    @ReqMapping("/delete")
    public Object delete(EquipmentClassify equipmentClassify) {
        if (equipmentClassify != null) {
            equipmentClassify.setDeleted(Boolean.TRUE);
            jdbcSession.updateById(equipmentClassify);
        }
        return Result.success();
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(EquipmentClassify equipmentClassify) {

        // 增加校验不允许有相同的分类名称
        EquipmentClassify item = jdbcSession.findOne(EquipmentClassify.class)
                .eq("deviceClassifyName", equipmentClassify.getDeviceClassifyName())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前分类名称，请修改后重试");
        }
        //插入分类
        jdbcSession.insert(equipmentClassify);
        return Result.success(equipmentClassify);
    }


    @ReqMapping("/get")
    public Object get(EquipmentClassify equipmentClassify) {
        return Result.success(equipmentClassify);
    }
}
